/**
 *博尔信基础开发平台
 *<br> 版权声明：厦门博尔信软件有限公司版权所有,违者必究
 *<br> Copyright:  Copyright (c) 2012
 *<br> Company:pearlsoft
 *<br> @author 蒋勇华
 *<br> 2012-5-7
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.pearlsoft.webapp.sysmgr.org.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.pearlsoft.commons.utils.UUIDUtil;
import com.pearlsoft.util.StringUtils;
import com.pearlsoft.webapp.core.WebException;
import com.pearlsoft.webapp.core.dao.RecrodRowMapper;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.service.BaseSimpleJdbcService;

/**
 *描述： 
 *
 * 
 */
@Service
public class OrgService extends BaseSimpleJdbcService{

	private final String generalFields = " {0}.ORG_ID,{0}.ORG_SIMPLE_NAME, {0}.ORG_FULL_NAME, {0}.ORG_PARENT_ID,{0}.ORG_ORDER,{0}.C_STATUS,{0}.C_REMARK";
	private final RecrodRowMapper generalRowMapper = OrgRowMapperHelper
			.generalRowMapper();
	
	public boolean checkNameDup(String orgId,String orgName){
		String sql ="";
		Map<String,String> params = new HashMap<String,String>();
		if( StringUtils.isBlank(orgId)){
			sql =" select count(*) from SYS_ORG where ORG_FULL_NAME = :ORG_FULL_NAME";
			params.put("ORG_FULL_NAME", orgName);
		}else{
			sql =" select count(*) from SYS_ORG where ORG_FULL_NAME = :ORG_FULL_NAME and ORG_ID <> :ORG_ID";
			params.put("ORG_FULL_NAME", orgName);
			params.put("ORG_ID", orgId);
		}
		
		int count = this.getBaseSimpleJdbcDao().queryForInt(sql, params);
		return count > 0 ? true:false;
	}
	/***
	 * 根据ID获取实体
	 * 
	 * @param orgId
	 * @return
	 */
	public Record getById(String orgId) {
		String sql = "SELECT " + StringUtils.format(generalFields, "t")
				+ " FROM SYS_ORG t WHERE ORG_ID=:ORG_ID";
		Map<String, String> param = new HashMap<String, String>();
		param.put("ORG_ID", orgId);
		Record record = this.getBaseSimpleJdbcDao().queryOne(sql, param, generalRowMapper);
		return record;
	}
	
	public void deleteById(String orgId) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("ORG_ID", orgId);
		String sql = "delete from SYS_ORG where ORG_ID=:ORG_ID";
		String usql="update SYS_USER SET ORG_ID='' where ORG_ID=:ORG_ID";
		this.getBaseSimpleJdbcDao().update(sql, param);
		this.getBaseSimpleJdbcDao().update(usql, param);
	}
	
	public List<Record> getChildren(String parentId){
		String sql = "SELECT "+StringUtils.format(generalFields, "t")+" FROM SYS_ORG t WHERE ORG_PARENT_ID=:ORG_PARENT_ID ORDER BY ORG_ORDER";
		Map<String, String> param = new HashMap<String, String>();
		param.put("ORG_PARENT_ID", parentId);		
		return this.getBaseSimpleJdbcDao().query(sql,param, generalRowMapper);		
	}

	/***
	 * 得到新增部门的序号
	 * 
	 * @param parentId
	 * @return
	 */
	public int getNewOrgOrder(String parentId) {
		String sql = " select count(*) from SYS_ORG where ORG_PARENT_ID =:ORG_PARENT_ID  ";
		Map<String, String> param = new HashMap<String, String>();
		param.put("ORG_PARENT_ID", parentId);
		return this.getBaseSimpleJdbcDao().queryForInt(sql, param) + 1;
	}

	private Record getOrgByOrder(String parentId, int order) {
		String sql = "SELECT "
				+ StringUtils.format(generalFields, "m")
				+ " FROM SYS_ORG m WHERE ORG_PARENT_ID=:ORG_PARENT_ID and ORG_ORDER =:ORG_ORDER";
		Map<String, String> param = new HashMap<String, String>();
		param.put("ORG_PARENT_ID", parentId);
		param.put("ORG_ORDER", order + "");
		Record record = this.getBaseSimpleJdbcDao().queryOne(sql, param, generalRowMapper);
		return record;
	}

	private void setOrgOrder(String menuId, int order) {
		String sql = "update SYS_ORG set ORG_ORDER=:ORG_ORDER where ORG_ID =:ORG_ID";
		Map<String, String> param = new HashMap<String, String>();
		param.put("ORG_ORDER", order + "");
		param.put("ORG_ID", menuId);
		this.getBaseSimpleJdbcDao().update(sql, param);
	}

	/**
	 * 上移动
	 * 
	 * @param orgId
	 * @throws Exception
	 */
	public void orgUp(String orgId) throws Exception {
		Record current = this.getById(orgId);
		String parentId = (String) current.getValue("ORG_PARENT_ID");
		int order = (Integer) current.getValue("ORG_ORDER");
		Record upOrg = this.getOrgByOrder(parentId, order - 1);
		if (upOrg == null) {
			throw new WebException("已经是最顶端,不能上移");
		} else {
			String upMenuId = (String) upOrg.getValue("ORG_ID");
			this.setOrgOrder(orgId, order - 1);
			this.setOrgOrder(upMenuId, order);
		}
	}

	/***
	 * 下移
	 * 
	 * @param menuId
	 * @throws Exception
	 */
	public void orgDown(String menuId) throws Exception {
		Record current = this.getById(menuId);
		String parentId = (String) current.getValue("ORG_PARENT_ID");
		int order = (Integer) current.getValue("ORG_ORDER");
		Record down = this.getOrgByOrder(parentId, order + 1);
		if (down == null) {
			throw new WebException("已经是最底端,不能下移");
		} else {
			String downId = (String) down.getValue("ORG_ID");
			this.setOrgOrder(menuId, order + 1);
			this.setOrgOrder(downId, order);
		}
	}
	
	public void save(Record record) throws WebException{
		String objId = (String)record.getValue("ORG_ID");
		String orgName = (String)record.getValue("ORG_FULL_NAME");
		boolean isNameDup = this.checkNameDup(objId, orgName);
		if( isNameDup ){
			throw new WebException("部门名称已经存在");
		}
		if(StringUtils.isBlank(objId)){
			this.insertRecord(record);
		}else{
			this.updateRecord(record);
		}
	}
	
	private void insertRecord(Record record)  {
		String objId=	UUIDUtil.get();
		record.setValue("ORG_ID", objId);
		String sql = "insert into SYS_ORG(ORG_ID,ORG_SIMPLE_NAME,ORG_FULL_NAME,ORG_PARENT_ID,ORG_ORDER,C_STATUS,C_REMARK) " +
				"values(:ORG_ID,:ORG_SIMPLE_NAME,:ORG_FULL_NAME,:ORG_PARENT_ID,:ORG_ORDER,:C_STATUS,:C_REMARK)";
		String parentId = (String) record.getValue("ORG_PARENT_ID");
		int newOrder = this.getNewOrgOrder(parentId);		
		Map<String, String> param = new HashMap<String, String>();
		param.put("ORG_ID", (String) record.getValue("ORG_ID"));
		param.put("ORG_SIMPLE_NAME", (String) record.getValue("ORG_SIMPLE_NAME"));
		param.put("ORG_FULL_NAME", (String) record.getValue("ORG_FULL_NAME"));
		param.put("ORG_PARENT_ID", (String) record.getValue("ORG_PARENT_ID"));
		param.put("ORG_ORDER", newOrder+"");
		param.put("C_STATUS", (String) record.getValue("C_STATUS"));
		param.put("C_REMARK", (String) record.getValue("C_REMARK"));
		this.getBaseSimpleJdbcDao().update(sql, param);
	}
	
	private void updateRecord(Record record) {
		String sql = "update SYS_ORG set ORG_SIMPLE_NAME=:ORG_SIMPLE_NAME,ORG_FULL_NAME=:ORG_FULL_NAME,ORG_PARENT_ID=:ORG_PARENT_ID,ORG_ORDER=:ORG_ORDER,C_STATUS=:C_STATUS,C_REMARK=:C_REMARK where ORG_ID=:ORG_ID";
		Map<String, String> param = new HashMap<String, String>();
		param.put("ORG_SIMPLE_NAME", (String) record.getValue("ORG_SIMPLE_NAME"));
		param.put("ORG_FULL_NAME", (String) record.getValue("ORG_FULL_NAME"));
		param.put("ORG_PARENT_ID", (String) record.getValue("ORG_PARENT_ID"));
		param.put("ORG_ORDER", (String) record.getValue("ORG_ORDER"));
		param.put("C_STATUS", (String) record.getValue("C_STATUS"));
		param.put("C_REMARK", (String) record.getValue("C_REMARK"));
		param.put("ORG_ID", (String) record.getValue("ORG_ID"));
		this.getBaseSimpleJdbcDao().update(sql, param);
	}
	
}
